package gr.upatras.uniexamtimetable;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/*
 * Required Class for an exam.
 * An exam can require multiple types of classes or multiple
 * classes of the same type but with different other characteristics
 */
public class RequiredClass {

	private String type;
	private int quantity;

	/* 
	 * If completeUsage == TRUE, then the class is used completely
	 * If set to FALSE, then there are empty seats
	 */
	private boolean completeUsage;

	/*
	 * parallelExamination is the ability to use half of the
	 * RequiredClass for another exam IF completeUsage is set to 
	 * False 
	 */
	private boolean parallelExamination;

	public RequiredClass(String type, int quantity, boolean completeUsage,
			boolean parallelExamination) {
		this.type = type;
		this.quantity = quantity;
		this.completeUsage = completeUsage;
		this.parallelExamination = parallelExamination;
	}

	public RequiredClass() {
		this.type = "sample type";
		this.quantity = 0;
		this.completeUsage = false;
		this.parallelExamination = false;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public boolean isCompleteUsage() {
		return completeUsage;
	}

	public void setCompleteUsage(boolean completeUsage) {
		this.completeUsage = completeUsage;
	}

	public boolean isParallelExamination() {
		return parallelExamination;
	}

	public void setParallelExamination(boolean parallelExamination) {
		this.parallelExamination = parallelExamination;
	}

	public void PrintStatus() {
		System.out.println("Required Class:");
		System.out.println("Type: " + type + ", " 
				+ ", Quantity: " + quantity 
				+ ", Parallel Examination: " + parallelExamination 
				+ ", Complete Usage: " + completeUsage);
		System.out.println();
	}

	public String toString() {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(String.format("Type: %s, ", type));
		stringBuilder.append(String.format("Quantity: %d, ", quantity));
		stringBuilder.append(String.format("Complete usage: %b, ", completeUsage));
		stringBuilder.append(String.format("Parallel examination: %b", parallelExamination));
		return stringBuilder.toString();
	}

	/* Exports a list of RequiredClass() objects from a json file */ 
	public static void exportToJson(
			List<RequiredClass> requiredClasses, String filename) {

		BufferedWriter writer = null;
		Gson gson = new Gson();

		System.out.printf("Trying to export to %s ...%n", filename);
		
		try {
			writer = new BufferedWriter(new FileWriter(filename));

			/* ugly way to get the type of List<Room> */
			Type collectionType = new TypeToken<List<RequiredClass>>(){}.getType();
			gson.toJson(requiredClasses, collectionType, writer);
		} catch (IOException e) {
			System.err.printf("Sorry, couldn't export to %s.%n", filename);
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e){
				System.err.println("Sorry, couldn't properly close the buffered writer");
				e.printStackTrace();
			}
		}
		
		System.out.printf("Success: %d obcjects were exported.%n", requiredClasses.size());
	}	

	/* Imports a list of RequiredClass objects from a json file */
	public static List<RequiredClass> importFromJson(String filename) {

		BufferedReader reader = null;
		Gson gson = new Gson();
		List<RequiredClass> requiredClasses = null;
		
		System.out.printf("Trying to import from %s ...%n", filename);
		
		try {
			reader = new BufferedReader(new FileReader(filename));

			/* ugly way to get the type of List<Room> */
			Type collectionType = new TypeToken<List<RequiredClass>>(){}.getType();
			requiredClasses = gson.fromJson(reader, collectionType);
		} catch (IOException e) {
			System.out.printf("Sorry, couldn't import from %s.%n", filename);
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				System.err.println("Sorry, couldn't close the buffered reader.");
				e.printStackTrace();
			}
		}
		
		System.out.printf("Success: %d obcjects were imported.%n", requiredClasses.size());
		return requiredClasses;
	}

	
	
}
